#include "Game.h"
#include <strsafe.h>
// Game states
#include "Pause.h"
#include "Play.h"
#include "Win.h"
#include "Lose.h"

Game::Game(HINSTANCE hInstance, int nShowCmd)
  : Core(hInstance, nShowCmd),
  stateMachine(this)
{
	timeLeft = 190;
	target = 10000000;
	// Initialise managers
	EntityManager::init();
	MessageHandler::init();
	AnimationPlayer::AnimationPlayer();
	ForceRegistry::init();
	// Add game states
	stateMachine.addState("Pause", new Pause());
	stateMachine.addState("Play", new Play());
	stateMachine.addState("Win", new Win());
	stateMachine.addState("Lose", new Lose());
	stateMachine.changeState("Play");

	// CONSTRUCT STUFF...
}

Game::~Game()
{
}

void Game::initialise()
{
	// INITIALISE STUFF...
	D3DXCreateFont(device, 28, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &font);
}

void Game::loadContent()
{
	// LOAD STUFF...

	// Screens
	D3DXCreateTextureFromFileEx(device, L"screenPause.png",
		D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
		0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
		D3DX_DEFAULT, D3DX_DEFAULT,
		0, 0, 0, &screenPauseTexture);
	// Create pause screen
	screenPause = new Screen(NULL, screenPauseTexture, viewport);
	D3DXCreateTextureFromFileEx(device, L"screenWin.png",
		D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
		0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
		D3DX_DEFAULT, D3DX_DEFAULT,
		0, 0, 0, &screenWinTexture);
	// Create win screen.
	screenWin = new Screen(NULL, screenWinTexture, viewport);
	D3DXCreateTextureFromFileEx(device, L"screenLose.png",
		D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
		0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
		D3DX_DEFAULT, D3DX_DEFAULT,
		0, 0, 0, &screenLoseTexture);
	// Create next level screen.
	screenLose = new Screen(NULL, screenLoseTexture, viewport);

	//set positions for nodes
	//for player
	int offsetX = 150;
	int offsetY = 150;

	nodePositions[0] = D3DXVECTOR3(10, 10, 0);

	//for nutral
	nodePositions[1] = D3DXVECTOR3(offsetX+10, 10, 0);
	nodePositions[2] = D3DXVECTOR3(2*offsetX+10, 10, 0);
	nodePositions[3] = D3DXVECTOR3(3*offsetX+10, 10, 0);
	nodePositions[4] = D3DXVECTOR3(4*offsetX+10, 10, 0);
	nodePositions[5] = D3DXVECTOR3(5*offsetX+10, 10, 0);
	
	//2nd line
	nodePositions[6] = D3DXVECTOR3(10, 10+ offsetY, 0);
	nodePositions[7] = D3DXVECTOR3(1*offsetX+10, 10 + offsetY, 0);
	nodePositions[8] = D3DXVECTOR3(3*offsetX+10, 10 + offsetY, 0);
	nodePositions[9] = D3DXVECTOR3(4*offsetX+10, 10 + offsetY, 0);
	nodePositions[10] = D3DXVECTOR3(5*offsetX+10, 10 + offsetY, 0);
	
	//3nd line
	nodePositions[11] = D3DXVECTOR3(10, 10+ offsetY*2, 0);
	nodePositions[12] = D3DXVECTOR3(1*offsetX+10, 10 + offsetY*2, 0);
	nodePositions[13] = D3DXVECTOR3(2*offsetX+10, 10 + offsetY*2, 0);
	nodePositions[14] = D3DXVECTOR3(3*offsetX+10, 10 + offsetY*2, 0);
	nodePositions[15] = D3DXVECTOR3(4*offsetX+10, 10 + offsetY*2, 0);
	nodePositions[16] = D3DXVECTOR3(5*offsetX+10, 10 + offsetY*2, 0);
	nodePositions[17] = D3DXVECTOR3(6*offsetX+10, 10 + offsetY*2, 0);
	
	//4nd line
	nodePositions[18] = D3DXVECTOR3(10, 10+ offsetY*3, 0);
	nodePositions[19] = D3DXVECTOR3(3*offsetX+10, 10 + offsetY*3, 0);
	nodePositions[20] = D3DXVECTOR3(4*offsetX+10, 10 + offsetY*3, 0);
	nodePositions[21] = D3DXVECTOR3(5*offsetX+10, 10 + offsetY*3, 0);
	
	//5nd line
	nodePositions[22] = D3DXVECTOR3(offsetX+10, 10+ offsetY*4, 0);
	nodePositions[23] = D3DXVECTOR3(2*offsetX+10, 10 + offsetY*4, 0);
	nodePositions[24] = D3DXVECTOR3(3*offsetX+10, 10 + offsetY*4, 0);
	nodePositions[25] = D3DXVECTOR3(4*offsetX+10, 10 + offsetY*4, 0);
	
	nodePositions[26] = D3DXVECTOR3(7*offsetX+10, 10 + offsetY*2, 0);
	nodePositions[27] = D3DXVECTOR3(10, 10 + offsetY*4, 0);


	//Set up player node
	{
		// Create player animation pointer
		tmp[0] = new AnimationPlayer();
	
		// Load player idle texture
		D3DXCreateTextureFromFileEx(device, L"playerBaseIdleAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &playerBaseIdleAnim);


		// Create animation from texture & set attributes
		Animation* playerIdle = new Animation(playerBaseIdleAnim);
		// Add amination to anims
		tmp[0]->addAnimation("playerBaseIdleAnim", playerIdle);
		
		//playerBaseActiveAnim
		D3DXCreateTextureFromFileEx(device, L"playerBaseActiveAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &playerBaseActiveAnim);


		// Create animation from texture & set attributes
		Animation* playerActive = new Animation(playerBaseActiveAnim);
		// Add amination to anims
		tmp[0]->addAnimation("playerBaseActiveAnim", playerActive);

		node[0] = new Node("BaseNode", tmp[0], nodePositions[0], Node::PLAYER);
	}

	//Set up neutral nodes
	int limit = ALLNODES -2;
	for(int i = 1; i < limit ; i++)
	{
		// Create player animation pointer
		tmp[i] = new AnimationPlayer();
	
		// Load player nutralNodeAnim texture
		D3DXCreateTextureFromFileEx(device, L"nutralNodeAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &nutralNodeAnim[i-1]);


		// Create animation from texture & set attributes
		Animation* nutralNode = new Animation(nutralNodeAnim[i-1]);
		// Add amination to anims
		tmp[i]->addAnimation("nutralNodeAnim", nutralNode);
		
		//secured
		D3DXCreateTextureFromFileEx(device, L"securedNodeAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &securedNodeAnim[i-1]);


		// Create animation from texture & set attributes
		Animation* securedNode = new Animation(securedNodeAnim[i-1]);
		// Add amination to anims
		tmp[i]->addAnimation("securedNodeAnim", securedNode);
		
		//hackedNodeAnim
		D3DXCreateTextureFromFileEx(device, L"hackedNodeAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &hackedNodeAnim[i-1]);


		// Create animation from texture & set attributes
		Animation* hackedNode = new Animation(hackedNodeAnim[i-1]);
		// Add amination to anims
		tmp[i]->addAnimation("hackedNodeAnim", hackedNode);
		
		
		//hackingSecuredNodeAnim
		D3DXCreateTextureFromFileEx(device, L"hackingSecuredNodeAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &hackingSecuredNodeAnim[i-1]);


		// Create animation from texture & set attributes
		Animation* hackingSecuredNode = new Animation(hackingSecuredNodeAnim[i-1]);
		// Add amination to anims
		tmp[i]->addAnimation("hackingSecuredNodeAnim", hackingSecuredNode);
		
		
		//secureHackedNodeAnim
		D3DXCreateTextureFromFileEx(device, L"secureHackedNodeAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &secureHackedNodeAnim[i-1]);


		// Create animation from texture & set attributes
		Animation* secureHackedNode = new Animation(secureHackedNodeAnim[i-1]);
		// Add amination to anims
		tmp[i]->addAnimation("secureHackedNodeAnim", secureHackedNode);
		

		//secureHackedNodeAnim
		D3DXCreateTextureFromFileEx(device, L"hackingNeutralNodeAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &hackingNeutralNodeAnim[i-1]);


		// Create animation from texture & set attributes
		Animation* hackingNeutralNode = new Animation(hackingNeutralNodeAnim[i-1]);
		// Add amination to anims
		tmp[i]->addAnimation("hackingNeutralNodeAnim", hackingNeutralNode);


		//securingNeutralNodeAnim
		D3DXCreateTextureFromFileEx(device, L"securingNeutralNodeAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &securingNeutralNodeAnim[i-1]);


		// Create animation from texture & set attributes
		Animation* securingNeutralNode = new Animation(securingNeutralNodeAnim[i-1]);
		// Add amination to anims
		tmp[i]->addAnimation("securingNeutralNodeAnim", securingNeutralNode);

		node[i] = new Node(tmp[i], nodePositions[i], Node::NEUTRAL);
	}


	//n -2 is target
	{
		// Create player animation pointer
		int limit = ALLNODES-2; 
		tmp[limit] = new AnimationPlayer();
	
		// Load player finishBaseIdleAnim texture
		D3DXCreateTextureFromFileEx(device, L"finishBaseIdleAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &finishBaseIdleAnim);
		// Create animation from texture & set attributes
		Animation* finishBaseIdle = new Animation(finishBaseIdleAnim);
		// Add amination to anims
		tmp[limit]->addAnimation("finishBaseIdleAnim", finishBaseIdle);

		// Load player hackingFinishBaseAnim texture
		D3DXCreateTextureFromFileEx(device, L"hackingFinishBaseAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &hackingFinishBaseAnim);
		// Create animation from texture & set attributes
		Animation* hackingFinishBase = new Animation(hackingFinishBaseAnim);
		// Add amination to anims
		tmp[limit]->addAnimation("hackingFinishBaseAnim", hackingFinishBase);

		
		// Load player hackingFinishBaseAnim texture
		D3DXCreateTextureFromFileEx(device, L"hackedFinishBaseAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &hackedFinishBaseAnim);
		// Create animation from texture & set attributes
		Animation* hackedFinishBase = new Animation(hackedFinishBaseAnim);
		// Add amination to anims
		tmp[limit]->addAnimation("hackedFinishBaseAnim", hackedFinishBase);


		// Load player hackingFinishBaseAnim texture
		D3DXCreateTextureFromFileEx(device, L"securingFinishBaseAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &securingFinishBaseAnim);
		// Create animation from texture & set attributes
		Animation* securingFinishBase = new Animation(securingFinishBaseAnim);
		// Add amination to anims
		tmp[limit]->addAnimation("securingFinishBaseAnim", securingFinishBase);


		// Load player hackingFinishBaseAnim texture
		D3DXCreateTextureFromFileEx(device, L"securedFinishBaseAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &securedFinishBaseAnim);
		// Create animation from texture & set attributes
		Animation* securedFinishBase = new Animation(securedFinishBaseAnim);
		// Add amination to anims
		tmp[limit]->addAnimation("securedFinishBaseAnim", securedFinishBase);


		node[limit] = new Node(tmp[limit], nodePositions[limit], Node::FINISH);
	}

	//Set up opponent node
	{
		// Create player animation pointer
		int limit = ALLNODES-1;
		tmp[limit] = new AnimationPlayer();
	
		// Load player idle texture
		D3DXCreateTextureFromFileEx(device, L"enemyBaseIdleAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &enemyBaseIdleAnim);


		// Create animation from texture & set attributes
		Animation* enemyBaseIdle = new Animation(enemyBaseIdleAnim);
		// Add amination to anims
		tmp[limit]->addAnimation("enemyBaseIdleAnim", enemyBaseIdle);
		
		//playerBaseActiveAnim
		D3DXCreateTextureFromFileEx(device, L"enemyBaseActiveAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &enemyBaseActiveAnim);


		// Create animation from texture & set attributes
		Animation* enemyBaseActive = new Animation(enemyBaseActiveAnim);
		// Add amination to anims
		tmp[limit]->addAnimation("enemyBaseActiveAnim", enemyBaseActive);

		node[limit] = new Node(tmp[limit], nodePositions[limit], Node::ENEMY);

	}
	AnimationPlayer* cursorAnims = new AnimationPlayer();
	
	// Load animations and setup Player entity.
	{
		// Load idle cursor texture
		D3DXCreateTextureFromFileEx(device, L"playerIdleAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &playerIdleAnim);
		// Create animation from texture
		Animation* playerIdle = new Animation(playerIdleAnim);
		// Add animation to player
		cursorAnims->addAnimation("Idle", playerIdle);
		// Load rotarting cursor texture
		D3DXCreateTextureFromFileEx(device, L"playerCursorAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &playerCursorAnim);
		// Create animation for texture
		Animation* playerCursor = new Animation(playerCursorAnim);
		// Add to Animation player
		cursorAnims->addAnimation("Focus", playerCursor);
		// Load target cursor texture
		D3DXCreateTextureFromFileEx(device, L"playerTargetAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &playerTargetAnim);
		// Create animation from texture
		Animation* playerTarget = new Animation(playerTargetAnim);
		// Add animation to player
		cursorAnims->addAnimation("Target", playerTarget);
	}
	AnimationPlayer* enimyCursorAnims = new AnimationPlayer();


	// Load animations and setup Enemy entity.
	{
		// Load idle cursor texture
		D3DXCreateTextureFromFileEx(device, L"enemyIdleAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &enemyIdleAnim);
		// Create animation from texture
		Animation* enemyIdle = new Animation(enemyIdleAnim);
		// Add animation to enemy
		enimyCursorAnims->addAnimation("Idle", enemyIdle);
		// Load rotarting cursor texture
		D3DXCreateTextureFromFileEx(device, L"enemyCursorAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &enemyCursorAnim);
		// Create animation for texture
		Animation* enemyCursor = new Animation(enemyCursorAnim);
		// Add to Animation enemy
		enimyCursorAnims->addAnimation("Focus", enemyCursor);
		// Load target cursor texture
		D3DXCreateTextureFromFileEx(device, L"enemyTargetAnim.png",
			D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
			0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
			D3DX_DEFAULT, D3DX_DEFAULT,
			0, 0, 0, &enemyTargetAnim);
		// Create animation from texture
		Animation* enemyTarget = new Animation(enemyTargetAnim);
		// Add animation to enemy
		enimyCursorAnims->addAnimation("Target", enemyTarget);
	}

	//add linking of nodes
	
	//adding id 1,6 to id 0 - always zero player
	links[0].push_back(node[1]);
	links[0].push_back(node[6]);

	//adding id 0, 2  to 1
	links[1].push_back(node[0]);
	links[1].push_back(node[2]);
	links[1].push_back(node[7]);

	//adding id 1,3to 2
	links[2].push_back(node[1]);
	links[2].push_back(node[3]);

	//adding id 4 and 1 to 3
	links[3].push_back(node[2]);
	links[3].push_back(node[4]);
	links[3].push_back(node[8]);

	links[4].push_back(node[3]);
	links[4].push_back(node[5]);

	links[5].push_back(node[4]);
	links[5].push_back(node[10]);

	links[6].push_back(node[0]);
	links[6].push_back(node[11]);

	links[7].push_back(node[12]);

	links[8].push_back(node[3]);
	links[8].push_back(node[14]);
	links[8].push_back(node[9]);

	links[9].push_back(node[8]);
	links[9].push_back(node[10]);
	links[9].push_back(node[15]);

	links[10].push_back(node[5]);
	links[10].push_back(node[16]);

	links[11].push_back(node[12]);

	
	links[12].push_back(node[13]);
	links[12].push_back(node[7]);
	links[12].push_back(node[11]);


	links[13].push_back(node[14]);
	links[13].push_back(node[12]);
	
	links[14].push_back(node[13]);
	links[14].push_back(node[18]);
	links[14].push_back(node[19]);
	links[14].push_back(node[15]);

	links[15].push_back(node[14]);
	links[15].push_back(node[9]);
	links[15].push_back(node[16]);

	links[16].push_back(node[10]);
	links[16].push_back(node[17]);
	links[16].push_back(node[24]);

	links[17].push_back(node[26]);
	links[17].push_back(node[16]);

	links[18].push_back(node[11]);
	links[18].push_back(node[27]);

	links[19].push_back(node[14]);
	links[19].push_back(node[20]);
	links[19].push_back(node[24]);

	links[20].push_back(node[19]);
	links[20].push_back(node[21]);
	links[20].push_back(node[25]);

	links[21].push_back(node[20]);
	links[21].push_back(node[16]);

	links[22].push_back(node[27]);
	links[22].push_back(node[23]);

	links[23].push_back(node[22]);
	links[23].push_back(node[24]);

	links[23].push_back(node[22]);
	links[23].push_back(node[24]);

	links[24].push_back(node[25]);
	links[24].push_back(node[23]);
	links[24].push_back(node[19]);

	links[25].push_back(node[24]);
	links[25].push_back(node[20]);

	links[27].push_back(node[18]);
	links[27].push_back(node[22]);



	//adding id 2 to 5 - last element enemy
	//links[5].push_back(node[2]);
	links[5].push_back(node[3]);
	
	for(int i = 0; i < ALLNODES; i++)
	{
		node[i]->addConnectedNodes(&links[i]);
	}

	// Create the player entity
	player = new Player(cursorAnims, node[0]);
	enemy = new Enemy(enimyCursorAnims, node[27]);
}

void Game::unloadContent()
{
	//Need to update that later on

}

void Game::update(float frameTime)
{
	if(timeLeft <= 0 )
	{
		if(player->getScore() >= target)
		{
			stateMachine.changeState("Win");
		}else {
			stateMachine.changeState("Lose");
		
		}
	}else {
		if(stateMachine.getCurrentState() == stateMachine.getState("Play"))
			timeLeft = timeLeft - frameTime;
		

	}
	// AUDIO TEST // When Space Bar is pressed play Zap sound.
	if (inputHandler->isKeyDown(DIK_Z))
		audioState.pSoundBank->Play( audioState.iZap, 0, 0, NULL );

	// When Esc is enter pause screen if it is pressed a second time exit the game.
	if (inputHandler->isKeyDown(DIK_ESCAPE) && !inputHandler->wasKeyDown(DIK_ESCAPE) && stateMachine.getCurrentState() == stateMachine.getState("Play"))
		stateMachine.changeState("Pause");
	else if (inputHandler->isKeyDown(DIK_RETURN) && !inputHandler->wasKeyDown(DIK_RETURN) && stateMachine.getCurrentState() == stateMachine.getState("Pause"))
		stateMachine.changeState("Play");
	else if (inputHandler->isKeyDown(DIK_ESCAPE) && !inputHandler->wasKeyDown(DIK_ESCAPE) && stateMachine.getCurrentState() == stateMachine.getState("Pause"))
		PostQuitMessage(0);
	else if (inputHandler->isKeyDown(DIK_ESCAPE) && !inputHandler->wasKeyDown(DIK_ESCAPE) && stateMachine.getCurrentState() == stateMachine.getState("Win"))
		PostQuitMessage(0);
	else if (inputHandler->isKeyDown(DIK_ESCAPE) && !inputHandler->wasKeyDown(DIK_ESCAPE) && stateMachine.getCurrentState() == stateMachine.getState("Lose"))
		PostQuitMessage(0);
	
	// Update StateMachine and hence managers, dependant on game state.
	stateMachine.update(frameTime);

	// Check for input.
	inputHandler->poll();
	// Make sounds.
	audioState.pEngine->DoWork();
}

void Game::render()
{	
	// Turn on ambient lighting.
//	device->SetRenderState(D3DRS_AMBIENT, 0xffffffff);

	// Clear buffers ready to render next frame.
	device->Clear(0, NULL, D3DCLEAR_TARGET,
				  D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);
	
	device->BeginScene();
	sprite->Begin(D3DXSPRITE_ALPHABLEND);
//------Begin scene-------------------------------------------------------------

	// DRAW STUFF...
	stateMachine.draw(sprite);

//------End scene---------------------------------------------------------------
	
	static wchar_t buffer[256];
	wsprintf(buffer, L" Target: %i \n GreanPeace: %i\n Evil Corp: %i \n Time Left: %i", this->target, player->getScore(), 0 - player->getScore(), (int)timeLeft);
	static RECT scoreBoard = { 1000, 500, 0,0};

	font->DrawText(sprite, buffer, -1, &scoreBoard, DT_NOCLIP, D3DCOLOR_XRGB(255,255,255));

	sprite->End();
	
	
	device->EndScene();
	// Draw to back buffer & screen.
	device->Present(0, 0, 0, 0);
}